using System;
using System.Runtime.InteropServices;

namespace WinApp.Services
{
    /// <summary>
    /// 鼠标操作辅助类
    /// </summary>
    public class MouseHelper
    {
        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int x, int y);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_WHEEL = 0x0800;
        private const int WHEEL_DELTA = 120; // 标准滚轮增量

        /// <summary>
        /// 在指定坐标位置执行鼠标左键点击
        /// </summary>
        /// <param name="x">屏幕X坐标</param>
        /// <param name="y">屏幕Y坐标</param>
        public static void Click(int x, int y)
        {
            // 将鼠标移动到指定位置
            SetCursorPos(x, y);

            // 模拟鼠标左键按下和释放
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        }

        /// <summary>
        /// 在指定位置执行鼠标滚轮滚动
        /// </summary>
        /// <param name="x">屏幕X坐标</param>
        /// <param name="y">屏幕Y坐标</param>
        /// <param name="scroll">滚动值，正值向下滚动，负值向上滚动，0不滚动</param>
        public static void Scroll(int x, int y, int scroll)
        {
            // 将鼠标移动到指定位置
            SetCursorPos(x, y);

            if (scroll != 0)
            {
                // 将scroll值转换为滚轮增量
                int wheelDelta = - scroll * WHEEL_DELTA;
                mouse_event(MOUSEEVENTF_WHEEL, x, y, wheelDelta, 0);
            }
        }
    }
}
